package socketmina.client;

import org.apache.mina.core.service.IoConnector;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Scanner;

/**
 * @ClassName TcpClient
 * @Description TODO
 * @Author yuanweimin
 * @Date 2018/8/23 20:04
 * @Version 1.0
 **/
public class TcpClient {

    public static void main(String[] args) {
        IoConnector connector = new NioSocketConnector();
        connector.setConnectTimeoutMillis(10000);
        connector.getFilterChain().addLast("codec",new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("utf-8")
        , LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));

        for (int i = 0; i < 10; ++i){
            Scanner scanner = new Scanner(System.in);
            String na = scanner.nextLine();
            connector.setHandler(new TcpClientHandler(na + " quit"));
            connector.connect(new InetSocketAddress("localhost",9124));
        }
       // connector.setHandler(new TcpClientHandler("你好\n 好好好"));
       // connector.connect(new InetSocketAddress("localhost",9124));
    }
}
